from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('community', '0001_initial'),
    ]

    operations = [
        migrations.RunSQL(
            sql="""
                SET @constraint_name = (
                    SELECT CONSTRAINT_NAME 
                    FROM information_schema.KEY_COLUMN_USAGE 
                    WHERE TABLE_SCHEMA = DATABASE() 
                    AND TABLE_NAME = 'xiangsu_posts' 
                    AND COLUMN_NAME = 'user_id' 
                    AND REFERENCED_TABLE_NAME = 'auth_user'
                    LIMIT 1
                );
                SET @sql = IF(@constraint_name IS NOT NULL, 
                    CONCAT('ALTER TABLE `xiangsu_posts` DROP FOREIGN KEY `', @constraint_name, '`'),
                    'SELECT 1'
                );
                PREPARE stmt FROM @sql;
                EXECUTE stmt;
                DEALLOCATE PREPARE stmt;
            """,
            reverse_sql="",
        ),
    ]